#include "./strutils.h"
#include <windows.h>

using namespace std;
using namespace MiniUI;

//Unicode
void Unicode::WideStringToUTF8String(const wchar_t* in, size_t len, std::string& out)
{
    char* pBuf = new char[len * 3 + 1];
    memset(pBuf, 0, len * 3 + 1);
    ::WideCharToMultiByte(CP_UTF8, 0, in, len, pBuf, len * 3, NULL, NULL);
    out = pBuf;
    delete[] pBuf;
}

void Unicode::UTF8StringToWideString(const char* in, size_t len, std::wstring& out)
{
    wchar_t* pBuf = new wchar_t[len + 1];
    memset(pBuf, 0, (len + 1) * sizeof(wchar_t));
    ::MultiByteToWideChar(CP_UTF8, 0, in, len, pBuf, len * sizeof(wchar_t));
    out = pBuf;
    delete [] pBuf;
}

void Unicode::UTF8Encode(const wchar_t* in, size_t len, std::string& out)
{
    WideStringToUTF8String(in, len, out);
}

void Unicode::UTF8Decode(const char* in, size_t len, std::wstring& out)
{
    UTF8StringToWideString(in, len, out);
}

void Unicode::UTF8Encode(const wchar_t* in, std::string& out)
{
    WideStringToUTF8String(in, wcslen(in), out);
}

void Unicode::UTF8Decode(const char* in, std::wstring& out)
{
    UTF8StringToWideString(in, strlen(in), out);
}